//
//  ViewController.m
//  location
//
//  Created by dllo on 15/11/16.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>

//  需要遵守CLLocationManagerDelegate协议
@interface ViewController ()<CLLocationManagerDelegate, MKMapViewDelegate>//第一个是定位位置的协议
@property(nonatomic, strong)CLLocationManager *locMgr;
@property(nonatomic, strong)MKMapView *mapView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor grayColor];
    [self.view addSubview:self.mapView];
    //     请求用户授权定位
    if ([self.locMgr respondsToSelector:@selector(requestAlwaysAuthorization)]) {
        [self.locMgr requestAlwaysAuthorization];
    }
    
    //  判断用户定位服务是否开启
    if ([CLLocationManager locationServicesEnabled]) {
        //  开始定位用户的位置
        [self.locMgr startUpdatingLocation];
        //  每隔多少米定位一次
        self.locMgr.distanceFilter = kCLDistanceFilterNone; //  这里设置为任何移动都会进行定位
        //  设置定位的精度, 一般精准度越高, 越耗电(这里设置为精准度最高的, 适用于导航应用)
        self.locMgr.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
        NSLog(@"已定位");
    } else {
        NSLog(@"未授权定位");
        //  不能定位用户的位置
        //  1. 提醒用户检查当前的网络状态
        //  2. 提醒用户打开定位开关
        UIAlertController *alertView = [UIAlertController alertControllerWithTitle:@"无法定位" message:@"请检查定位服务是否打开或网络服务是否良好" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
        [alertView addAction:okAction];
        [self presentViewController:alertView animated:YES completion:nil];
        
    }
    
    
    //  测试方法, 计算两个位置之间的距离
    [self countDistance];
    
    //  打开系统地图进行导航定位
    CLLocationCoordinate2D startCoor = self.mapView.userLocation.coordinate;
    CLLocationCoordinate2D endCoor = CLLocationCoordinate2DMake(startCoor.latitude + 0.01, startCoor.longitude + 0.01);
    
    MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
    MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:endCoor addressDictionary:nil]];
    toLocation.name = @"to name";
    
    [MKMapItem openMapsWithItems:@[currentLocation, toLocation] launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving, MKLaunchOptionsShowsTrafficKey:                                             [NSNumber numberWithBool:YES]}];
    
}

- (MKMapView *)mapView {
    if (!_mapView) {
        self.mapView = [[MKMapView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        self.mapView.mapType = MKMapTypeSatelliteFlyover;
        self.mapView.delegate = self;
        
    }
    return _mapView;
}

#pragma mark    - CLLocationManagerDelegate
//  当定位到用户位置时, 就会调用(调用频率比较频繁)
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
    NSLog(@"dfasdfa");
    //  locations数组里存放的是CLLocation对象, 一个CLLocation对象就代表着一个位置
    CLLocation *loc = [locations firstObject];
    
    //  纬度:
    //  经度:
    NSLog(@"纬度 = %f,经度  = %f", loc.coordinate.latitude, loc.coordinate.longitude);
    NSLog(@"%ld", locations.count);
    
    //  停止更新位置 (如果定位服务不需要实时更新的话, 那么应该停止位置的更新)
//    [self.locMgr stopUpdatingLocation];
}

- (void)countDistance {
    //  根据经纬度创建两个位置对象
    CLLocation *loc1 = [[CLLocation alloc] initWithLatitude:40 longitude:116];
    CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:41 longitude:116];
    //  计算两个位置之间的距离
    CLLocationDistance distance = [loc1 distanceFromLocation:loc2];
    NSLog(@"(%@) 和 (%@) 的距离 = %fM", loc1, loc2, distance);
}
#pragma mark    - 懒加载
- (CLLocationManager *)locMgr {
    if (!_locMgr) {
        //  1. 创建位置管理器
        self.locMgr = [[CLLocationManager alloc] init];
        //  2. 设置代理
        _locMgr.delegate = self;
    }
    return _locMgr;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
